package com.konsung.ksdc.manager.appmain;

import android.content.Intent;
import android.os.Bundle;
import android.view.View;
import android.widget.ImageView;
import android.widget.RelativeLayout;
import android.widget.TextView;

import com.google.gson.Gson;
import com.konsung.ksdc.R;
import com.konsung.ksdc.bean.netbean.RegisterUserNetBean;
import com.konsung.ksdc.common.database.DatabaseHelper;
import com.konsung.ksdc.common.utils.Constants;
import com.konsung.ksdc.common.utils.PackageUtils;
import com.konsung.ksdc.common.utils.StringUtils;
import com.konsung.ksdc.config.MyApplication;
import com.konsung.ksdc.config.NetConfig;
import com.konsung.ksdc.manager.advice.AdviceActivity;
import com.konsung.ksdc.manager.community.ChatActivity;
import com.konsung.ksdc.manager.community.MeIndexActivity;
import com.konsung.ksdc.manager.machine.MachineTeachingActivity;
import com.konsung.ksdc.manager.machine.MyMachineActivity;
import com.konsung.ksdc.manager.machine.NetPicActivity;
import com.konsung.ksdc.manager.shop.MyIntegralActivity;
import com.konsung.ksdc.manager.shop.MyOrderActivity;
import com.konsung.ksdc.manager.user.ForgetPassCheckActivity;
import com.squareup.picasso.Picasso;

import java.sql.SQLException;
import java.util.List;

import butterknife.Bind;

/**
 * 会员中心界面
 * Created by andy on 2017/4/13.
 */

public class MeFragment extends BaseFragment implements View.OnClickListener {

    @Bind(R.id.rl_my_machine)
    RelativeLayout rlMyMachine;
    @Bind(R.id.rl_version_container)
    RelativeLayout rlVersionContainer;
    @Bind(R.id.rl_machine_teaching)
    RelativeLayout rlMachingTeaching;
    @Bind(R.id.rl_advice)
    RelativeLayout rlAdvice;
    @Bind(R.id.rl_change_pwd)
    RelativeLayout rlChangePwd;
    @Bind(R.id.rl_order)
    RelativeLayout rlOrder;
    @Bind(R.id.rl_service)
    RelativeLayout rlService;
    @Bind(R.id.rl_user_msg)
    RelativeLayout rlUserMsg;
    @Bind(R.id.tab_tv_title)
    public TextView mTvTitle;
    @Bind(R.id.iv_add_new)
    protected ImageView ivAddNew;
    @Bind(R.id.rl_back)
    protected RelativeLayout mIvBack;
    /*用户信息区*/
    @Bind(R.id.iv_user_head)
    ImageView ivUserHead;
    @Bind(R.id.tv_user_nick)
    TextView tvUserNick;
    @Bind(R.id.tv_user_account)
    TextView tvUserAccount;
    @Bind(R.id.rl_version_code)
    TextView tvVersionCode;
    @Bind(R.id.rl_integral)
    RelativeLayout rlIntegral;

    public static final int REFRESH_USER_INFO = 7; // 更新修改后的用户信息
    private String picUrl;

    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
    }

    @Override
    public void onResume() {
        super.onResume();
    }

    @Override
    public void onDestroy() {
        super.onDestroy();
    }

    @Override
    protected void initEvent() {
        initUserMsg();
        rlMyMachine.setOnClickListener(this);
        rlMachingTeaching.setOnClickListener(this);
        rlAdvice.setOnClickListener(this);
        rlChangePwd.setOnClickListener(this);
        rlOrder.setOnClickListener(this);
        rlService.setOnClickListener(this);
        rlUserMsg.setOnClickListener(this);
        ivUserHead.setOnClickListener(this);
        rlIntegral.setOnClickListener(this);
    }

    /**
     * 初始化用户信息
     */
    private void initUserMsg() {
        mTvTitle.setText(R.string.title_activity_me);
        ivAddNew.setVisibility(View.GONE);
        mIvBack.setVisibility(View.GONE);
        if (NetConfig.currentUser == null) {
            return;
        }

        picUrl = StringUtils.appendPicPath(NetConfig.currentUser.getUserPic());
        Picasso.with(getActivity())
                .load(picUrl)
                .placeholder(R.drawable.pic_talkbox_sel)
                .error(R.drawable.pic_talkbox_sel)
                .into(ivUserHead);

        tvUserNick.setText(NetConfig.currentUser.getNickName());
        tvVersionCode.setText(PackageUtils.getVersionName(MyApplication.getContext()));
        String userNum = getResources().getString(R.string.app_user_no);
        tvUserAccount.setText(userNum + " " + NetConfig.currentUser.getUserName());
    }

    @Override
    protected int getLayoutId() {
        return R.layout.fragment_me;
    }

    @Override
    public void onClick(View v) {
        switch (v.getId()) {
            case R.id.rl_my_machine:
                // 我的一体机
                Intent intent1 = new Intent(getActivity(), MyMachineActivity.class);
                startActivity(intent1);
                break;
            case R.id.rl_machine_teaching:
                Intent intent2 = new Intent(getActivity(), MachineTeachingActivity.class);
                startActivity(intent2);
                break;
            case R.id.rl_advice:
                Intent intent3 = new Intent(getActivity(), AdviceActivity.class);
                startActivity(intent3);
                break;
            case R.id.rl_change_pwd:
                Intent intent4 = new Intent(getActivity(), ForgetPassCheckActivity.class);
                startActivity(intent4);
                break;
            case R.id.rl_order:
                Intent intent5 = new Intent(getActivity(), MyOrderActivity.class);
                startActivity(intent5);
                break;
            case R.id.rl_service:
                // 客服咨询
                openSaleServiceChat();
                break;
            case R.id.rl_user_msg:
                Intent intent7 = new Intent(getActivity(), MeIndexActivity.class);
                startActivityForResult(intent7, REFRESH_USER_INFO);
                break;
            case R.id.iv_user_head:
                Intent intent8 = new Intent(getActivity(), NetPicActivity.class);
                intent8.putExtra(NetPicActivity.PIC_URL, picUrl);
                startActivity(intent8);
                break;
            case R.id.rl_integral:
                Intent intent9 = new Intent(getActivity(), MyIntegralActivity.class);
                startActivity(intent9);
                break;
            default:
                break;
        }
    }

    @Override
    public void onActivityResult(int requestCode, int resultCode, Intent data) {
        super.onActivityResult(requestCode, resultCode, data);
        switch (requestCode) {
            case REFRESH_USER_INFO:
                initUserMsg();
                break;
            default:
                break;
        }
    }

    /**
     * 打开在线客服聊天界面
     */

    private void openSaleServiceChat() {
        try {
            List<RegisterUserNetBean> users = DatabaseHelper.getHelper(getActivity())
                    .getDao(RegisterUserNetBean.class).queryForEq("userName", Constants.TOPIC_KSKF);
            if (users.isEmpty()) {
                return;
            }

            Intent intent = new Intent(getActivity(), ChatActivity.class);
            intent.putExtra("userId", users.get(0).getUserId());
            intent.putExtra("topicName", users.get(0).getUserName());
            intent.putExtra("nickName", users.get(0).getNickName());
            intent.putExtra("picPath", users.get(0).getUserPic());
            Gson gson = new Gson();
            intent.putExtra("data", gson.toJson(users.get(0)));
            startActivity(intent);
        } catch (SQLException e) {
            e.printStackTrace();
        }
    }
}